/*
 * Copyright (c) 2012 Andrea De Rinaldis <ozioso at ipignoli.dyndns.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dyndns.ipignoli.droidcruise.notifications;

import org.dyndns.ipignoli.droidcruise.util.MyContext;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;


public class MyNotification extends Notification{
  private int    id;
  private String title, text;
  private Intent notificationIntent;

  public MyNotification(MyNotification n){
    this(n.id,n.icon,n.tickerText,n.title,n.text,n.notificationIntent);
  }

  public MyNotification(int id, int icon, CharSequence tickerText,
      String title, String text, Intent notificationIntent){
    super(icon, tickerText, 1l);
    flags |= Notification.FLAG_AUTO_CANCEL;
    setLatestEventInfo(MyContext.getInstance().getContext(), title, text,
        PendingIntent.getActivity(MyContext.getInstance().getContext(), 0,
            notificationIntent, 0));
    this.id = id;
    this.title = title;
    this.text = text;
    this.notificationIntent = notificationIntent;
  }

  public int getId(){
    return id;
  }

  public CharSequence getTitle(){
    return title;
  }

  public CharSequence getText(){
    return text;
  }

  public void setText(String text){
    this.text = text;
  }

  public void send(){
    setLatestEventInfo(MyContext.getInstance().getContext(), title, text,
        PendingIntent.getActivity(MyContext.getInstance().getContext(), 0,
            notificationIntent, 0));
    when = System.currentTimeMillis();
    MyNotificationManager.getInstance().notify(id, this);
  }

  public void cancel(){
    MyNotificationManager.getInstance().cancel(id, this);
  }

  @Override
  public int hashCode(){
    final int prime = 31;
    int result = 1;
    result = prime * result + ((getText() == null) ? 0 : getText().hashCode());
    result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj){
    if(this == obj)
      return true;
    if(obj == null)
      return false;
    if(!(obj instanceof MyNotification))
      return false;
    MyNotification other = (MyNotification)obj;
    if(getText() == null){
      if(other.getText() != null)
        return false;
    }
    else if(!getText().equals(other.getText()))
      return false;
    if(getTitle() == null){
      if(other.getTitle() != null)
        return false;
    }
    else if(!getTitle().equals(other.getTitle()))
      return false;
    return true;
  }
}
